import 'dart:math';

import 'package:get/get.dart';

class AntiShakeController extends GetxController {
  //TODO: Implement AntiShakeController

  final count = 0.obs;

  final type = 0.obs;

  final name = "".obs;

  @override
  void onInit() {
    super.onInit();

    ever(count, (callback) {
      if (type != 1) {
        return;
      }
      print("ever----$count callback=${callback}");
    });

    // 监听多个值,当它们发生改变的时候调用
    everAll([count, name], (callback) {
      if (type != 2) {
        return;
      }
      print("everAll----$count callback=${callback}");
    });

    // count值改变时调用,只执行一次
    once(count, (callback) {
      // if (type != 3) {
      //   return;
      // }
      print("once----$count callback=${callback}");
    });

    // 用户停止打字时1秒后调用,主要是防DDos
    debounce(count, (callback) {
      if (type != 4) {
        return;
      }
      print("debounce----$count callback=${callback}");
    });

    // 忽略3秒内的所有变动
    interval(count, (callback) {
      if (type != 5) {
        return;
      }
      print("interval----$count callback=${callback}");
    });
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void increment() => count.value++;

  void setType(int type) {
    this.type.value = type;
  }

  void changeName() {
    this.name.value = "hello${Random().nextInt(100).toString()}";
  }
}
